package cn.wolfcode.interceptor;

import cn.wolfcode.domain.Result;
import cn.wolfcode.util.JwtUtils;
import com.alibaba.fastjson.JSON;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.minidev.json.JSONArray;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


@Component
public class LoginCheck implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String aaa = request.getHeader("token");
        try {
            JwtUtils.parseJwt(aaa);
            return true;
        } catch (Exception e) {
            response.setContentType("application/json;charset=utf-8");
            Result result=Result.nologin();

            String jsonString= JSON.toJSONString(result);
            response.getWriter().write(jsonString);
            return false;
        }

    }
}
